<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="info.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据表格 -->
      <el-table :data="orderlist" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="order_pay">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === 1"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editdialogShow(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-outline"
              size="mini"
              @click="showProgress(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页模块 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单对话框 -->
    <el-dialog
      title="编辑订单"
      :visible.sync="editdialogVisible"
      width="50%"
      @close="editdialogClose"
    >
      <el-form
        :model="editForm"
        ref="editRef"
        :rules="editrules"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="editForm.address1"
            :options="cityData"
            :props="cityProps"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOder">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="prodialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline :reverse="true">
          <el-timeline-item
            v-for="(activity, index) in progresslist"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 导入城市数据
import cityData from './citydata'

export default {
  name: 'Order',

  data() {
    return {
      // 订单数据列表
      orderlist: [],
      // 订单数据的请求参数
      info: {
        query: '',
        pagenum: 1,
        pagesize: 1
      },
      total: 0,
      editdialogVisible: false,
      // 编辑订单地址表单
      editForm: {
        address1: [],
        address2: ''
      },
      // 编辑订单地址表单规则
      editrules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 城市数据
      cityData,
      // 级联选择器的验证规则
      cityProps: { expandTrigger: 'hover' },
      // 显示物流进度对话框的隐藏
      prodialogVisible: false,
      // 物流信息列表
      progresslist: []
    }
  },
  created() {
    // 获取订单列表
    this.getOrser()
  },
  methods: {
    // 请求订单数据列表
    async getOrser() {
      const { data: res } = await this.$http.get('orders', {
        params: this.info
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.orderlist = res.data.goods
      this.total = res.data.total
      console.log(res.data)
    },
    handleSizeChange(newsize) {
      this.info.pagesize = newsize
      this.getOrser()
    },
    handleCurrentChange(newpage) {
      this.info.pagenum = newpage
      this.getOrser()
    },
    // 点击编辑，显示编辑订单地址对话框
    editdialogShow(row) {
      this.editdialogVisible = true
    },
    // 关闭修改对话框，重置编辑表单
    editdialogClose() {
      this.$refs.editRef.resetFields()
      console.log(this.$refs)
    },

    // 显示物流进度对话框
    async showProgress(row) {
      this.prodialogVisible = true
      // 获取物流信息
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meata.msg)
      console.log(res.data)
      this.progresslist = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
